package com.example.leoss.denglu_zhuce;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leoss.R;
import com.example.leoss.ShouyeActivity;

public class DengluActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText denglu_ed1;
    private EditText denglu_ed2;
    private TextView denglu_tv1;
    private TextView denglu_tv2;
    private Button denglu_bt1;
    public SharedPreferences sp;
    private Button denglu_bt2;
    private MyOkHttp myOkHttp;
    public boolean clop = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_denglu);
        initView();
    }

    private void initView() {
        denglu_ed1 = (EditText) findViewById(R.id.denglu_ed1);
        denglu_ed2 = (EditText) findViewById(R.id.denglu_ed2);
        denglu_tv1 = (TextView) findViewById(R.id.denglu_tv1);
        denglu_tv1.setOnClickListener(this);
        denglu_tv2 = (TextView) findViewById(R.id.denglu_tv2);
        denglu_tv2.setOnClickListener(this);
        denglu_bt1 = (Button) findViewById(R.id.denglu_bt1);
        denglu_bt1.setOnClickListener(this);
        denglu_bt2 = (Button) findViewById(R.id.denglu_bt2);
        denglu_bt2.setOnClickListener(this);
        myOkHttp = new MyOkHttp(this);

        sp = getSharedPreferences("confi", MODE_PRIVATE);
        String name1 = sp.getString("myname", null);
        String pass1 = sp.getString("mypass", null);

        denglu_ed1.setText(name1);
        denglu_ed2.setText(pass1);

    }

    private void submit() {
        // validate
        final String ed1 = denglu_ed1.getText().toString().trim();
        if (TextUtils.isEmpty(ed1)) {
            Toast.makeText(this, "ed1不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        final String ed2 = denglu_ed2.getText().toString().trim();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String s = myOkHttp.dengLu(ed1, ed2);
                if (s.equals("1")) {
//                    Toast.makeText(DengluActivity.this, "登录成功", Toast.LENGTH_SHORT).show();


                    Boolean S = true;
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("myname", ed1);
                    editor.putString("mypass", ed2);
                    editor.putBoolean("denglu", S);
                    editor.apply();
                    finish();

                } else {
//                    Toast.makeText(DengluActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        }).start();


    }

    private int random() {
        int a = (int) (Math.random() * 10000);
        return a > 999 && a < 10000 ? a : random();
    }
    /***
     *用户按下返回键时先提示，然后确认是否退出
     */
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            if((System.currentTimeMillis()-exitTime) > 2000)
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else
            {
                Intent home = new Intent(Intent.ACTION_MAIN);
                home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                home.addCategory(Intent.CATEGORY_HOME);
                startActivity(home);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.denglu_bt1:
                submit();
                break;
            case R.id.denglu_bt2:
                finish();
                break;
            case R.id.denglu_tv1:
//                intent.setClass(this, ZhuceActivity.class);
//                startActivity(intent);
                Toast.makeText(DengluActivity.this,"功能开发中",Toast.LENGTH_SHORT).show();
                break;
            case R.id.denglu_tv2:
                intent.setClass(this, ZhuceActivity.class);
                startActivity(intent);
                break;
        }
    }
}
